#define _CRT_SECURE_NO_WARNINGS 1
#include"SList.h"
SLTNode* BuySListData(SLTtype x)
{
	SLTNode* newnode = malloc(sizeof(SLTNode));
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}
void PushBack(SLTNode** pphead, SLTtype x)
{
	if (*pphead == NULL)
	{
		*pphead = BuySListData(x);
	}
	else 
	{
		SLTNode* newnode = BuySListData(x);
		SLTNode* tail = *pphead;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}
		tail->next = newnode;
	}
}
void ListPrint(SLTNode* phead)
{
	SLTNode* tail = phead;
	while (tail != NULL)
	{
		printf("%d->", tail->data);
		tail = tail->next;
	}
	printf("NULL\n");
}
void pushfront(SLTNode** pphead, SLTtype x)
{
	if (*pphead == NULL)
	{
		*pphead = BuySListData(x);
		(*pphead)->next = NULL;
		return;
	}
	SLTNode* newnode = BuySListData(x);
	newnode->next = *pphead;
	*pphead = newnode;
}
void popback(SLTNode** pphead)
{
	if(*pphead == NULL)
	{
		return;
	}
	else if ((*pphead)->next == NULL)
	{
		free(*pphead);
		*pphead = NULL;
		return;
	}
	else
	{
		SLTNode* prev = NULL;
		SLTNode* tile = *pphead;
		while (tile->next != NULL)
		{
			prev = tile;
			tile = tile->next;
		}
		free(tile);
		prev->next = NULL;
	}
}
void popfront(SLTNode** pphead)
{
	if (*pphead == NULL)
	{
		return;
	}
	else if ((*pphead)->next == NULL)
	{
		free(*pphead);
		*pphead = NULL;
		return;
	}
	else
	{
		SLTNode* tile;
		tile = *pphead;
		*pphead = tile->next;
		free(tile);
	}
}
SLTNode* FindPoi(SLTNode** pphead, SLTtype poi)
{
	SLTNode* newnode = *pphead;
	while (newnode)
	{
		if (newnode->data == poi)
			return newnode;
		newnode = newnode->next;
	}
	return NULL;
}
void Insert(SLTNode** pphead, SLTtype poi, SLTtype x)
{
	if ((*pphead)->data == x)
	{
		pushfront(pphead, x);
	}
	else
	{
		SLTNode* tile = *pphead;
		SLTNode* newpoi = BuySListData(x);
		while (tile->next != FindPoi(pphead, poi))
		{
			tile = tile->next;
		}
		newpoi->next = FindPoi(pphead, poi);
		tile->next = newpoi;
	}
}
void Erase(SLTNode** pphead, SLTtype poi)
{
	if (*pphead == NULL)
		return;
	else
	{
		SLTNode* newnode = FindPoi(pphead, poi);
		SLTNode* tile = *pphead;
		while (tile->next != newnode)
		{
			tile = tile->next;
		}
		tile->next = newnode->next;
		free(newnode);
	}
}
void SlistDestroy(SLTNode** plist)
{
	if ((*plist) != NULL)
	{
		SlistDestroy(&((*plist)->next));
		free(*plist);
		*plist = NULL;
	}
}
void SlistDelete(SLTNode** plist)
{
	SLTNode* cur = NULL;
	while (*plist != NULL)
	{
		cur = (*plist)->next;
		free(*plist);
		*plist = NULL;
		*plist = cur;
	}
}